package com.example.yupao.controller;

import com.example.yupao.common.Result;
import com.example.yupao.constant.UserConstant;
import com.example.yupao.enums.StatusEnum;
import com.example.yupao.exception.BusinessException;
import com.example.yupao.model.domain.User;
import com.example.yupao.model.request.*;
import com.example.yupao.model.vo.UserVO;
import com.example.yupao.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Author hhf
 * @Date 2024/8/26
 * @Description
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/update")
    public Result updateUser(@RequestBody UserUpdateRequest userUpdateRequest,HttpServletRequest request){
        if (userUpdateRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR,"请求参数有误");
        }

        return Result.success(userService.updateByCondition(userUpdateRequest,request),"更新成功");
    }

    @GetMapping("/search/tags")
    public Result searchUsersByTags(SearchUsersByTagsRequest searchUsersByTagsRequest){

        List<User> userList = userService.searchUsersByTags(searchUsersByTagsRequest);

        return Result.success(userList);
    }


    @PostMapping("/register")
    public Result register(@RequestBody RegisterRequest registerRequest) {

        if (registerRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR,"请求参数有误");
        }
        String account = registerRequest.getAccount();
        String password = registerRequest.getPassword();
        String checkPassword = registerRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(account, password, checkPassword)) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR);
        }

        return Result.success(userService.register(account, password, checkPassword));
    }

    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest, HttpServletRequest request) {
        if (loginRequest == null) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR);
        }
        String account = loginRequest.getAccount();
        String password = loginRequest.getPassword();
        if (StringUtils.isAnyBlank(account, password)) {
            throw new BusinessException(StatusEnum.PARAMS_ERROR);
        }

        return Result.success(userService.login(account, password, request),"登录成功");

    }


    @GetMapping("/current")
    public Result getCurrentUser(HttpServletRequest request) {

        return Result.success(userService.getCurrentUser(request));
    }

    @GetMapping("/recommend")
    public Result recommendUsers(UserSearchRequest userSearchRequest,HttpServletRequest request) {

        return Result.success(userService.recommendUsers(userSearchRequest,request));
    }

    @GetMapping("/search")
    public Result searchUsers(UserSearchRequest userSearchRequest, HttpServletRequest request) {

        if (!isAdmin(request)) {
            throw new BusinessException(StatusEnum.NO_AUTH);
        }

        return Result.success(userService.search(userSearchRequest));
    }

    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {

        userService.logout(request);
        return Result.success(null);

    }


    private boolean isAdmin(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATUS);
        User user = (User) userObj;

        if (user == null || UserConstant.ADMIN_ROLE != user.getRole()) {
            return false;
        }
        return true;
    }

    @DeleteMapping
    public Result deleteUser(long id, HttpServletRequest request) {

        if (!isAdmin(request)) {
            throw new BusinessException(StatusEnum.NO_AUTH);
        }

        if (id < 0) {
            throw new BusinessException(StatusEnum.SIMPLE_ERROR,"用户不存在");
        }

        return Result.success(userService.removeById(id));
    }

    @GetMapping("/match")
    public Result matchTeam(@RequestParam(required = false) Long num,HttpServletRequest request){
        List<UserVO> userVOList = userService.matchTeam(num,request);
        return Result.success(userVOList);
    }
}
